<?php

namespace App\Model;

use EasySwoole\Jwt\Exception;
use EasySwoole\Jwt\Jwt;
use EasySwoole\ORM\AbstractModel;
use Throwable;

/**
 * Class UserModel
 * Create With Automatic Generator
 * @property $id
 * @property $nick_name
 * @property $sex
 * @property $headimg_url
 * @property $phone
 * @property $real_name
 * @property $withdrawal_account
 * @property $create_time
 * @property $update_time
 * @property $frid
 * @property $unique_id
 * @property $is_real
 */
class UserModel extends AbstractModel
{
    protected $tableName = 'bee_user';

    protected $primaryKey = 'id';

    protected $autoTimeStamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    private $tokenSecret = 'sOzViJSxDtYx3pgnqdoboANTIvTDNiDgLixFyoOdqAWtJYOicGVFmdWNVvOxDkMS';

    private $tokenIss = 'beeapi.beepunch.cn';

    private $tokenExp = 180000;

    /**
     * 通过主键条件查找用户第三方登录信息
     * @param string $field
     * @return UserOpenModel|null
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function getOneByPrimaryKey($field = '*'): ?UserModel
    {
        /** @var UserModel $info */
        $info = $this->field($field)->get($this->id);
        return $info;
    }

    /**
     * 生成JWT access_token
     * @param string $sub
     * @return bool|string
     */
    public function makeToken(string $sub = ''): ?string
    {
        $jwtObject = Jwt::getInstance()
            ->setSecretKey($this->tokenSecret) // 秘钥
            ->publish();

        $jwtObject->setAlg('HMACSHA256'); // 加密方式
        $jwtObject->setAud($this->id); // 用户
        $jwtObject->setExp(time() + $this->tokenExp); // 过期时间
        $jwtObject->setIat(time()); // 发布时间
        $jwtObject->setIss($this->tokenIss); // 发行人
        $jwtObject->setJti(md5(uniqid('bee_' . $this->id . '_' . $this->unique_id . '_' . time()))); // jwt id 用于标识该jwt
        $jwtObject->setNbf(time()); // 在此之前不可用
        $jwtObject->setSub('beeToken'); // 主题

        // 自定义数据
        $jwtObject->setData([
            'unique_id' => $this->unique_id
        ]);

        // 最终生成的token
        return $jwtObject->__toString();
    }

    /**
     * 返回token过期属性
     * @return int|null
     */
    public function getTokenExp(): ?int
    {
        return $this->tokenExp;
    }

    /**
     * 解密token
     * @param string $token
     * @return int|null
     * @throws Exception
     */
    public function decryptToken(string $token = ''): ?int
    {
        $jwtObject = Jwt::getInstance()->decode($token);
        $status = $jwtObject->getStatus();
        switch ($status) {
            case  1:
//                echo '验证通过';
//                $jwtObject->getAlg();
//                $jwtObject->getAud();
//                $jwtObject->getData();
//                $jwtObject->getExp();
//                $jwtObject->getIat();
//                $jwtObject->getIss();
//                $jwtObject->getNbf();
//                $jwtObject->getJti();
//                $jwtObject->getSub();
//                $jwtObject->getSignature();
//                $jwtObject->getProperty('alg');
                if ($this->tokenIss != $jwtObject->getIss()) {
                    return -2;
                } elseif ('beeToken' != $jwtObject->getSub()) {
                    return -2;
                } else {
                    return intval($jwtObject->getAud());
                }
                break;
            case  -1:
//                echo '无效';
                return -1;
                break;
            case  -2:
//                echo 'token过期';
                return -2;
                break;
            default:
                return -2;
                break;
        }
    }
}